package com.cloud.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/6/28 15:00
 * @Desc
 * @Remarks
 */
@Slf4j
public class LogInterceptor implements HandlerInterceptor {
    /**
     * 预处理回调方法
     * true表示继续流程, false表示流程中断
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("请求的URL:" + request.getRequestURL());
        return true;
    }


    /**
     * 后处理回调方法，实现处理器的后处理（但在渲染视图之前），此时可以通过modelAndView对模型数据进行处理或对视图进行处理
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("postHandle 请求URL的后置处理:" + request.getRequestURL());
    }


    /**
     * 整个请求处理完毕回调方法，即在视图渲染完毕时回调
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        if (null != ex) {
            log.info("处理结束了，异常信息{}" + ex);
        }
    }
}